<?php

namespace app\api\controller;

use app\admin\model\Feedback;
use app\admin\model\Homepage;
use app\admin\model\Problem;
use app\common\controller\Api;
use app\common\library\Token;
use think\captcha\Captcha;
use think\config\driver\Ini;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        Token::delete("72a6efa9-28e4-43f4-a70f-bd6a0b385789");
        $this->success('请求成功');
    }

    public function lastversion()
    {
        if(input('type') == 1){
            $latestVersion = Db::name('version')
                ->where('platform', 'andriod')
                ->where('mode', '1')
                ->order('newversion', 'desc')
                ->find();
            $url = 'https://xckj-auto-file-res.oss-cn-shenzhen.aliyuncs.com/file/learnstu.apk';
            if($latestVersion){
                $url = cdnurl($latestVersion['downloadurl'],true);
            }
        }else{
            $latestVersion = Db::name('teacherversion')
                ->where('platform', 'andriod')
                ->order('newversion', 'desc')
                ->where('mode', '1')
                ->find();
            $url = 'https://xckj-auto-file-res.oss-cn-shenzhen.aliyuncs.com/file/teacher.apk';
            if($latestVersion){
                $url = cdnurl($latestVersion['downloadurl'],true);
            }
        }
        $this->success('成功',['downurl'=>$url]);
    }

    public function getIndexSetting(){
        $homepageModel = new Homepage();
        $is_vip = false;
        if($this->auth->id){
            $user = \app\admin\model\User::get($this->auth->id);
            if($user->vip_end_time > time()){
                $is_vip = true;
            }
        }
        if($is_vip){
            $kewaikuozhan = $homepageModel->where(['type'=>4])->order('sort','desc')->select();
            $banner = $homepageModel->where(['type'=>1])->order('sort','desc')->select();
            $jingangqu = $homepageModel->where(['type'=>2])->order('sort','desc')->select();
            $qupeiyin = $homepageModel->where(['type'=>3])->order('sort','desc')->select();
            $jiaocai = $homepageModel->with('text_books')->where(['type'=>5])->order('sort','desc')->select();
        }else{
            $kewaikuozhan = $homepageModel->where(['type'=>4])->order('sort','desc')->select();
            $banner = $homepageModel->where(['type'=>1])->order('sort','desc')->select();
            $jingangqu = $homepageModel->where(['type'=>2])->order('sort','desc')->select();
            $qupeiyin = $homepageModel->where(['type'=>3])->order('sort','desc')->select();
            $jiaocai = $homepageModel->with('text_books')->where(['type'=>5])->order('sort','desc')->select();
        }
        $messageModel = new \app\api\model\Message();
        $notRead = $messageModel->where(['is_read'=>1])->count();
       $this->success('success',[
           'banner'=>$banner,
           'jingangqu'=>$jingangqu,
           'qupeiyin'=>$qupeiyin,
           'kewaikuozhan'=>$kewaikuozhan,
           'isVip'=>$is_vip,
           'bookid' => '3',
           'notRead'=>$notRead,
           'jiaocai'=>$jiaocai
       ]);
    }

    public function getSetting(){
        $mobile =config('site.mobile');
        $wechat_no =config('site.wechat_no');
        $wechat_img =cdnurl(config('site.wechat_img'),true);
        $is_eyeshield = \app\admin\model\User::where(['id'=>$this->auth->id])->value('is_eyeshield')? true:false;
        $this->success('success',[
            'mobile'=>$mobile,
            'is_eyeshield'=>$is_eyeshield,
            'wechat_no'=>$wechat_no,
            'wechat_img'=>$wechat_img
        ]);
    }
    public function getProblem(){
        $problemModel= new Problem();
        $type = $this->request->param('type',2);
        $list = $problemModel->where(['type'=>$type])->order('sort','desc')->select();
        $this->success('success',$list);
    }

    public function getAbout(){
       $data = [
           'use_agreement' => config('site.use_agreement'),
           'privacy_agreement' => config('site.privacy_agreement'),
           'protect_agreement' => config('site.protect_agreement'),
           'abstract' => config('site.abstract'),
           'user_inventory' => config('site.user_inventory'),
           'sdk_inventory' => config('site.sdk_inventory'),
           'auth_inventory' => config('site.auth_inventory'),
           'cancel_statement'=>config('site.cancel_statement')
       ];
       $this->success('success',$data);
    }

    public function addFeedback(){
        $prams = $this->request->post();
        $model = new Feedback();
        $prams['user_id'] = $this->auth->id;
        $res = $model->save($prams);
        if ($res) {
            $this->success('success');
        } else {
            $this->error('error');
        }
    }
    public function connectUs(){
        $data = [
            'education_consultant' => config('site.education_consultant'),
            'education_mobile' => config('site.education_mobile'),
            'mobile' => config('site.mobile'),
        ];
        $this->success('success',$data);
    }
    public function getTeacherAbout(){
        $data = [
            'teacher_use_agreement' => config('site.teacher_use_agreement'),
            'teacher_privacy_agreement' => config('site.teacher_privacy_agreement'),
            'teacher_protect_agreement' => config('site.teacher_protect_agreement'),
            'teacher_abstract' => config('site.teacher_abstract'),
            'teacher_user_inventory' => config('site.teacher_user_inventory'),
            'teacher_sdk_inventory' => config('site.teacher_sdk_inventory'),
            'teacher_auth_inventory' => config('site.teacher_auth_inventory'),
            'teacher_cancel_statement'=>config('site.teacher_cancel_statement')
        ];
        $this->success('success',$data);
    }

    public function checkcat(){
        $data['captcha'] = $this->request->post('captcha');
        $data['captcha_token'] = $this->request->post('captcha_token');
        $Captcha = new Captcha();
        $result = $Captcha->check($data['captcha'],$data['captcha_token']);
        if (!$result) {
            $this->error("图形验证码错误");
        }
    }
}
